//
//  CommentTableView.m
//  Custom_Weibo
//
//  Created by LEO on 12/15/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "CommentTableView.h"

@implementation CommentTableView

#pragma mark - INITIALIZATION_METHODS
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        //  Create header view.
        [self createHeaderView];
        self.delegate = self;
        self.dataSource = self;
        
        UINib *nibFile = [UINib nibWithNibName:@"CommentCell" bundle:nil];
        [self registerNib:nibFile forCellReuseIdentifier:kCommentCellID];
    }
    return self;
}

- (void)setWeiboModel:(WeiboModel *)weiboModel {
    if (_weiboModel != weiboModel) {
        _weiboModel = weiboModel;
    }
    
    //  1)Rearrange frame.
    WeiboViewLayoutFrame *layoutFrame = [[WeiboViewLayoutFrame alloc] init];
    
    layoutFrame.isDetail = YES;
    layoutFrame.weiboModel = weiboModel;
    
    _weiboView.layoutFrame = layoutFrame;
    _weiboView.frame = layoutFrame.mainFrame;
    
    _weiboView.top = _userView.bottom + 5;
    
    //  2)User view.
    _userView.weiboModel = weiboModel;
    
    //  3)Header view.
    _tableHeaderView.height = _weiboView.bottom;
    self.tableHeaderView = _tableHeaderView;
    
}

#pragma mark - HEADER_VIEW
- (void)createHeaderView {
    //  1)Create header view for manager the user view and weibo view.
    CGRect tableFrame = CGRectMake(0, 0, self.width, 0);
    _tableHeaderView = [[UIView alloc] initWithFrame:tableFrame];
    _tableHeaderView.backgroundColor = [UIColor clearColor];
    
    //  2)User xib to create user view
    _userView = [[[NSBundle mainBundle] loadNibNamed:@"UserView"
                                               owner:self
                                             options:nil] lastObject];
    _userView.width = kScreenWidth;
    _userView.backgroundColor = kRGB(0, 0, 0, 0.1);
    [_tableHeaderView addSubview:_userView];
    
    //  3)Create weibo view.
    _weiboView = [[HomeView alloc] initWithFrame:CGRectZero];
    _weiboView.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_tableHeaderView addSubview:_weiboView];
    
}

#pragma mark - UITABLEVIEW_DELEGATE
//  Set table header view.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    //  1)Create section table view.
    CGRect sectionFrame = CGRectMake(0, 0, tableView.width, 40);
    UIView *sectionHeaderView = [[UIView alloc] initWithFrame:sectionFrame];
    sectionHeaderView.backgroundColor = kRGB(0, 0, 0, 0.1);
    
    //  2)Comment label.
    CGRect commentFrame = CGRectMake(10, 10, 300, 20);
    UILabel *countLabel = [[UILabel alloc] initWithFrame:commentFrame];
    countLabel.backgroundColor = [UIColor clearColor];
    countLabel.font = [UIFont boldSystemFontOfSize:16.0f];
    countLabel.textColor = [UIColor blackColor];
    
    //  3)Comment counts.
    countLabel.text = [NSString stringWithFormat:@"Comment:%li", _commentCount];
    
    [sectionHeaderView addSubview:countLabel];
    return sectionHeaderView;
}

//  Set each section header's height.
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    return 40;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    return 400;
//}

//  Set each row's height.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentModel *model = _commentListArray[indexPath.row];
    CGFloat height = [CommentCell getCommentHeight:model];
    return height;
}

//  Set section number.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//  Set row number.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _commentListArray.count;
}

//  Cell setting.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kCommentCellID
                                                        forIndexPath:indexPath];
    cell.commentModel = _commentListArray[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self deselectRowAtIndexPath:indexPath animated:YES];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
